package com.kelaite.procurement.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.entity.TSalesOrder;
import com.kelaite.procurement.repository.SalesOrderRepository;
import com.kelaite.procurement.service.SalesOrderService;

@Service
@Transactional
public class SalesOrderServiceImpl implements SalesOrderService{
  
	@Autowired
	private SalesOrderRepository salesOrderRepository; 
	
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public TSalesOrder save(TSalesOrder persistence) {
		return this.salesOrderRepository.save(persistence);
	}

	@Override
	public TSalesOrder getObject(Long id) {
		return this.salesOrderRepository.findOne(id);
	}

	@Override
	public List<TSalesOrder> getAll() {
		return this.salesOrderRepository.findAll();
	}

	@Override
	public void deleteByObject(TSalesOrder persistence) {
		this.salesOrderRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		this.salesOrderRepository.delete(id);
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public List<TSalesOrder> getByProperty(String property, Object value) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TSalesOrder.class).add(org.hibernate.criterion.Expression.eq(property, value)).add(org.hibernate.criterion.Expression.eq("isDelete", false));
		return criteria.list();
	}

	@Override
	public void updateSalesEstimate(Long salesOrderId, String evlautionDesc,String evaluationScore4,
			Date updateTime, String updateBy) {
		
		this.salesOrderRepository.updateSalesOrderEstimate(salesOrderId, evlautionDesc, Double.parseDouble(evaluationScore4),updateTime, updateBy);
	}

	@SuppressWarnings({ "deprecation", "unchecked" })
	@Transactional
	@Override
	public List<TSalesOrder> getByPropertyByStatus(String property,
			Object value, Short orderStatus) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria=null;
		//��ѯȫ������
		if(orderStatus==5)
		{
			criteria = session.createCriteria(TSalesOrder.class).add(org.hibernate.criterion.Expression.eq(property, value)).
					add(org.hibernate.criterion.Expression.eq("isDelete", false));
		}
		else//���״̬�鶩��
		{
			Object[] orderStatusArray=new Object[2];
			if(orderStatus==2)//已完成订单
			{
				orderStatusArray[0]=(short)2;
				orderStatusArray[1]=(short)3;
			}
			else//待履行订单
			{
				orderStatusArray[0]=(short)0;
				orderStatusArray[1]=(short)1;
			}
			criteria = session.createCriteria(TSalesOrder.class).add(org.hibernate.criterion.Expression.eq(property, value)).
					add(org.hibernate.criterion.Expression.eq("isDelete", false)).
					add(org.hibernate.criterion.Expression.in("orderStatus", orderStatusArray));
		}
		return criteria.list();
	}

	@Override
	public void updateSalesEstimateManyGrade(Long salesOrderId,
			String evlautionDesc, String evaluationScore2,
			String evaluationScore3, String evaluationScore4,
			String evaluationScore5, Date updateTime, String updateBy) {


		this.salesOrderRepository.updateSalesOrderEstimateManyGrade(salesOrderId, 
				evlautionDesc, 
				Double.parseDouble(evaluationScore2),
				Double.parseDouble(evaluationScore3),
				Double.parseDouble(evaluationScore4),
				Double.parseDouble(evaluationScore5),
				updateTime, updateBy);
		
	}

}
